package cn.edu.neusoft.yuejian12.Adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.yuejian12.Activity.ActivityInfoActivity;
import cn.edu.neusoft.yuejian12.Activity.TrainInfoActivity;
import cn.edu.neusoft.yuejian12.JavaBean.Activity;
import cn.edu.neusoft.yuejian12.R;
import cn.edu.neusoft.yuejian12.Service.ActivityService;

/**
 * Created by 木子李 on 2017/10/16.
 */

public class HactivityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    public HactivityAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }




    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.h_activity_card, parent, false);
        return new HactivityAdapter.ViewHolder(v);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Activity entity = (Activity) mDataList.get(position);
        if (null == entity)
            return;
        HactivityAdapter.ViewHolder viewHolder = (HactivityAdapter.ViewHolder) holder;

        viewHolder.activity_name.setText(entity.getActivity_name());

        viewHolder.activity_hits.setText(entity.getHits());
        String photo = entity.getThumb();
        if (photo != ""){
            Picasso.with(mContext)
                    .load("http://172.24.10.175/workout/Uploads/"+entity.getThumb())
                    .into(viewHolder.activity_pic);

            viewHolder.activity_pic.setScaleType(ImageView.ScaleType.FIT_XY);

        }

        if(viewHolder.activity_pic.getDrawable()==null){
            viewHolder.activity_pic.setImageResource(R.drawable.no_pic);
            viewHolder.activity_pic.setScaleType(ImageView.ScaleType.FIT_XY);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,ActivityInfoActivity.class);
                intent.putExtra("id",entity.getId());
                intent.putExtra("activity_name",entity.getActivity_name());
                mContext.startActivity(intent);
            }
        });

    }


    @Override
    public int getItemCount() {
        return mDataList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView activity_pic;
        TextView activity_name,activity_hits;




        public ViewHolder(View itemView) {
            super(itemView);

            activity_hits = (TextView) itemView.findViewById(R.id.h_activity_count);
            activity_name = (TextView) itemView.findViewById(R.id.h_activity_name);
            activity_pic = (ImageView) itemView.findViewById(R.id.h_activity_pic);


        }
    }
}
